package com.elitel.common.auth.aspect;

import com.elitel.common.auth.annotation.Permissions;
import com.elitel.common.core.domain.SysUserInfo;
import com.elitel.common.core.exception.GlobalException;
import com.elitel.common.core.log.enums.ResultCodeEnum;
import com.elitel.common.utils.SysUserUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;

/**
 * 操作日志记录处理
 *
 * @author mls
 */
@Aspect
@Component
public class PermissionsAspect {
    private static final Logger log = LoggerFactory.getLogger(PermissionsAspect.class);

//    SysUserUtils


    /**
     * 处理请求前执行
     */
    @Before(value = "@annotation(permissions)")
    public void boBefore(JoinPoint joinPoint, Permissions permissions) {

    }

    /**
     * 处理完请求后执行
     *
     * @param joinPoint 切点
     */
    @AfterReturning(pointcut = "@annotation(Permissions)", returning = "jsonResult")
    public void doAfterReturning(JoinPoint joinPoint, Permissions Permissions, Object jsonResult) {
        handleLog(joinPoint, Permissions, null, jsonResult);
    }

    /**
     * 拦截异常操作
     *
     * @param joinPoint 切点
     * @param e         异常
     */
    @AfterThrowing(value = "@annotation(permissions)", throwing = "e")
    public void doAfterThrowing(JoinPoint joinPoint, Permissions permissions, Exception e) {
        handleLog(joinPoint, permissions, e, null);
    }

    protected void handleLog(final JoinPoint joinPoint, Permissions permissions, final Exception e, Object jsonResult) {

        //若permissions不为空,则进行权限校验 通过后直接返回 不通过则记录日志 返回异常
        //若permissions为空,则对UserType进行校验 通过后直接返回 不通过则记录日志 返回异常
        String permissionType = permissions.permissionId();
        String userType = permissions.roleId();

        SysUserInfo userInfo = SysUserUtils.getUserInfo();
        if (!ObjectUtils.isEmpty(userInfo)) {
            String userPermissions = userInfo.getPermissions();
            if (!ObjectUtils.isEmpty(userPermissions)) {
                List<String> userPermissionList = Arrays.asList(userPermissions.split(","));
                if (!userPermissionList.contains(permissionType)) {
                    log.error("权限不足");
                    throw new GlobalException(ResultCodeEnum.PERMISSIONS_NOT_REQUIRE);
                }
            } else {
                String roleIds = userInfo.getRoleIds();
                if (!ObjectUtils.isEmpty(roleIds)) {
                    List<String> userRoleIds = Arrays.asList(roleIds.split(","));
                    if (!userRoleIds.contains(userType)) {
                        log.error("权限不足");
                        throw new GlobalException(ResultCodeEnum.PERMISSIONS_NOT_REQUIRE);
                    }
                }
            }
        }
    }

}
